package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletGrammarSpecificListInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_specific_grammar.view.*
import java.math.BigDecimal

/*
* 语法知识专项item
* */
class AibkGenerateSpecificGrammarAdapter(
        val activity: AppCompatActivity,
        val templetGrammarSpecificListInfor: TempletGrammarSpecificListInfor
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_specific_grammar, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val indexA = positionReturnIndexA(position)
        val indexB = positionReturnIndexB(position)

        val subTopicName = templetGrammarSpecificListInfor[indexA].SubTopicList[indexB].SubTopicName
        val isChecked = templetGrammarSpecificListInfor[indexA].SubTopicList[indexB].IsChecked
        val testLevel = templetGrammarSpecificListInfor[indexA].SubTopicList[indexB].TestLevel
        val masteryLevel = templetGrammarSpecificListInfor[indexA].SubTopicList[indexB].MasteryLevel
        val scoreRate = templetGrammarSpecificListInfor[indexA].SubTopicList[indexB].ScoreRate

        //显示语法名称
        holder.convertView.TvGrammarName.text = subTopicName
        //显示选中状态
        holder.convertView.LlGrammarSpecifiView.isSelected = isChecked
        holder.convertView.BtnAnalysis.isSelected = isChecked
        //显示测试概率
        if(testLevel == 1) {
            holder.convertView.TvTestLevel.text = "常考"
            holder.convertView.TvTestLevel.setTextColor(activity.resources.getColor(R.color.lancoooGreen))
        }else if(testLevel == 2) {
            holder.convertView.TvTestLevel.text = "次常考"
            holder.convertView.TvTestLevel.setTextColor(activity.resources.getColor(R.color.lancoooBlue))
        }else if(testLevel == 3) {
            holder.convertView.TvTestLevel.text = "一般"
            holder.convertView.TvTestLevel.setTextColor(activity.resources.getColor(R.color.lancoooRed))
        }
        //显示分值占比
        val scoreRateX = LancooMethod.getScoreRate(scoreRate,2)
        holder.convertView.TvScoreRate.text = scoreRateX
        //显示掌握程度
        if(masteryLevel == 1) {
            holder.convertView.TvMasteryLevel.text = "扎实"
            holder.convertView.TvMasteryLevel.setTextColor(activity.resources.getColor(R.color.lancoooGreen))
        }else if(masteryLevel == 2) {
            holder.convertView.TvMasteryLevel.text = "一般"
            holder.convertView.TvMasteryLevel.setTextColor(activity.resources.getColor(R.color.lancoooBlue))
        }else if(masteryLevel == 3) {
            holder.convertView.TvMasteryLevel.text = "较差"
            holder.convertView.TvMasteryLevel.setTextColor(activity.resources.getColor(R.color.lancoooRed))
        }

        holder.convertView.LlGrammarSpecifiView.setOnClickListener {
            holder.convertView.LlGrammarSpecifiView.isSelected = !holder.convertView.LlGrammarSpecifiView.isSelected
            holder.convertView.BtnAnalysis.isSelected = holder.convertView.LlGrammarSpecifiView.isSelected
            templetGrammarSpecificListInfor[indexA].SubTopicList[indexB].IsChecked = holder.convertView.LlGrammarSpecifiView.isSelected
        }

        holder.convertView.BtnAnalysis.setOnClickListener {
//            val dialogSpecificAnalysis = DialogSpecificAnalysis(activity,activity,templetGrammarSpecificListInfor,indexA,indexB,false)
//            dialogSpecificAnalysis.show()
            clickListener!!.onClickAnalysis(indexA,indexB,false)
        }
    }

    override fun getItemCount(): Int {
        return 3
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    //此处添加点击事件的回调方法
    private var clickListener: OnClickListener? = null

    interface OnClickListener {
        fun onClickAnalysis(indexA:Int,indexB:Int,isAllAnalysis:Boolean)
    }

    fun setClickAnalysisListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

    private fun positionReturnIndexA(position:Int):Int{
        var indexA = 0
        var currentPosition = -1
        val parentListSize = templetGrammarSpecificListInfor.size
        outLoop@for(i in 0 until parentListSize){
            val childListSize = templetGrammarSpecificListInfor[i].SubTopicList.size
            for(j in 0 until childListSize){
                currentPosition += 1
                if(currentPosition == position){
                    indexA = i
                    break@outLoop
                }
            }
        }
        return  indexA
    }

    private fun positionReturnIndexB(position:Int):Int{
        var indexB = 0
        var currentPosition = -1
        val parentListSize = templetGrammarSpecificListInfor.size
        outLoop@for(i in 0 until parentListSize){
            val childListSize = templetGrammarSpecificListInfor[i].SubTopicList.size
            for(j in 0 until childListSize){
                currentPosition += 1
                if(currentPosition == position){
                    indexB = j
                    break@outLoop
                }
            }
        }
        return  indexB
    }
}